//
//  LCHomePassageTableViewCell.m
//  段子手
//
//  Created by dllo on 16/3/18.
//  Copyright © 2016年 JokesTeam. All rights reserved.
//

#import "LCHomePassageTableViewCell.h"


@interface LCHomePassageTableViewCell ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) UIImageView *imgVUser;
@property (nonatomic, retain) UILabel *labelUserName;
@property (nonatomic, retain) UILabel *labelText;
@property (nonatomic, retain) UIButton *buttonKind;
@property (nonatomic, retain) UIButton *buttonZan;
@property (nonatomic, retain) UILabel *labelZan;
@property (nonatomic, retain) UIButton *buttonLow;
@property (nonatomic, retain) UILabel *labelLow;
@property (nonatomic, retain) UIButton *buttonHot;
@property (nonatomic, retain) UILabel *labelHot;

@property (nonatomic, retain) UITableView *tableViewComment;

@property (nonatomic, assign) CGRect rectText;
@property (nonatomic, assign) CGRect rectButton;

@property (nonatomic, assign)CGRect rectCommentText;

@property (nonatomic, assign)CGFloat tableViewCommentH;


@end

@implementation LCHomePassageTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self create];
    }
    return self;
}


- (void)setModel:(LCHomeTableViewPassageModel *)model{
//    [super setModel:model];
//    LCHomeTableViewPassageModel *model = (LCHomeTableViewPassageModel *)model;
    
    _model = model;
    
    [self setModelValue];
    
//    [_tableViewComment reloadData];
//    [self reloadInputViews];
    
//    _block(_tableViewCommentH);
    
}


- (void)setModelValue{
    
//    获取评论tableView的自适应高度
    self.tableViewCommentH = 0;
    
    if ( self.model .comments.count != 0) {
        
            for (int i = 0; i <  self.model.comments.count; i++) {
                NSDictionary *arr  = [NSDictionary dictionaryWithDictionary: self.model .comments[i]];
                NSString *str = arr[@"text"];
                CGRect rect = [WxlAPPTools scripeSuitWidthWithString:str Font:13 Size:CGSizeMake(SCREENW - 20 - 60, 0)];
                _tableViewCommentH += rect.size.height + 50;
                
        }
    }

    
    
//    获取段子内容的自适应高度
    self.rectText = [WxlAPPTools scripeSuitWidthWithString: self.model.text Font:14 Size:CGSizeMake(SCREENW - 40, 0)];
    
    
    NSLog(@"%@",  self.model .category_name);
    self.rectButton = [WxlAPPTools scripeSuitWidthWithString: self.model.category_name Font:10 Size:CGSizeMake(0, 20)];
    
    
    [_imgVUser sd_setImageWithURL:[NSURL URLWithString: self.model.user[@"avatar_url"]]];
    
    _labelUserName.text =  self.model.user[@"name"];
    
    _labelText.text = self.model.text;
    
    [_buttonKind setTitle:self.model.category_name forState:UIControlStateNormal];
    
    
    _labelZan.text = [NSString stringWithFormat:@"%@", self.model.digg_count];
    
    _labelLow.text = [NSString stringWithFormat:@"%@", self.model.bury_count];
    
    _labelHot.text = [NSString stringWithFormat:@"%@", self.model.comment_count];
    
    if (_tableViewComment) {
        _tableViewComment.frame = CGRectMake(10, _buttonZan.frame.origin.y + _buttonZan.frame.size.height, SCREENW - 20, _tableViewCommentH);
        
    }
    
}



#pragma mark - 创建评论的tableView
- (void)createTableViewComment{
    
    if (self.tableViewComment) {
        [_tableViewComment removeFromSuperview];
    }
    
    self.tableViewComment = [[UITableView alloc]initWithFrame:CGRectMake(10, _buttonZan.frame.origin.y + _buttonZan.frame.size.height, SCREENW - 20, _tableViewCommentH) style:UITableViewStylePlain];
    
    _tableViewComment.bounces = NO;
    
    _tableViewComment.dataSource = self;
    _tableViewComment.delegate = self;
    
    [_tableViewComment registerClass:[LCContentTableViewCell class] forCellReuseIdentifier:@"commentCell"];
//    _tableViewComment.backgroundColor = [UIColor colorWithRed:0.4 green:0.9 blue:0.4 alpha:1];
    
//    self.tableViewCommentH = 0.0;
    
    [self.contentView addSubview:_tableViewComment];
    
    
}


#pragma mark - 评论tableView的协议方法
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LCContentTableViewCell *commentCell = [tableView dequeueReusableCellWithIdentifier:@"commentCell"];
    
    LCHomeCommentModel *modelC = [LCHomeCommentModel modelWithDic:self.model.comments[indexPath.row]];
    
//    NSLog(@"%@", modelC);
    
    commentCell.model = modelC;
    
//    commentCell.backgroundColor = [UIColor colorWithRed:0.8 green:0.4 blue:0.4 alpha:0.1];
    
    
    return commentCell;
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (self.model.comments.count > 2) {
        return 2;
    }else return self.model.comments.count;

}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSString *textStr = self.model.comments[indexPath.row][@"text"];
    
    self.rectCommentText = [WxlAPPTools scripeSuitWidthWithString:textStr Font:13 Size:CGSizeMake(SCREENW - 80, 0)];
    
//    NSLog(@"%f", 50 + _rectCommentText.size.height);
    
    return 50 + _rectCommentText.size.height;
}




- (void)create{
    
    self.imgVUser = [[UIImageView alloc]init];
    [self.contentView addSubview:_imgVUser];
    
    self.labelUserName = [[UILabel alloc]init];
    [self.contentView addSubview:_labelUserName];
    

    self.labelText = [[UILabel alloc]init];
    [self.contentView addSubview:_labelText];
    
    self.buttonKind = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_buttonKind];
    
    self.buttonZan = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_buttonZan];
    
    
    self.labelZan = [[UILabel alloc]init];
    [self.contentView addSubview:_labelZan];
    
    
    self.buttonLow = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview: _buttonLow];
    
    
    self.labelLow = [[UILabel alloc]init];
    [self.contentView addSubview:_labelLow];
    
    
    self.buttonHot = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.contentView addSubview:_buttonHot];
    
    self.labelHot = [[UILabel alloc]init];
    [self.contentView addSubview:_labelHot];
}


- (void)layoutSubviews{
    
    [super layoutSubviews];
    
//    用户头像
     _imgVUser.frame =CGRectMake(20, 20, 30, 30);
    
    _imgVUser.layer.cornerRadius = 15;
    _imgVUser.layer.masksToBounds = YES;
    
//    用户名
     _labelUserName.frame = CGRectMake(_imgVUser.frame.size.width + 20 + 20, 20, SCREENW -  _imgVUser.frame.size.width - 20 - 20 - 20, 30);
    
//    段子内容
    _labelText.frame = CGRectMake(20, _imgVUser.frame.size.height + _imgVUser.frame.origin.y + 15, SCREENW - 40, _rectText.size.height);
    _labelText.font = [UIFont systemFontOfSize:14];
    _labelText.numberOfLines = 100;
    
//    种类按钮
    _buttonKind.frame = CGRectMake(20, _labelText.frame.origin.y + _labelText.frame.size.height + 15, _rectButton.size.width + 20, 20);
    _buttonKind.layer.cornerRadius = 10;
    _buttonKind.layer.borderColor = [UIColor colorWithRed:0.42 green:0.33 blue:0.28 alpha:1].CGColor;
    _buttonKind.layer.borderWidth = 0.5;
    [_buttonKind setTitleColor:[UIColor colorWithRed:0.42 green:0.33 blue:0.28 alpha:1] forState:UIControlStateNormal];
    _buttonKind.titleLabel.font = [UIFont systemFontOfSize:10];

    
//    下面的点赞按钮
//    if (_model.comments.count != 0 ) {
//
//        _buttonZan.frame = CGRectMake(20, _tableViewComment.frame.origin.y + _tableViewCommentH + 5, 30, 30);
//        
//    }else{
        _buttonZan.frame = CGRectMake(20, _buttonKind.frame.origin.y + _buttonKind.frame.size.height +5, 30, 30);

//    }
    
//    _buttonZan.backgroundColor = [UIColor redColor];
    [_buttonZan setImage:[UIImage imageNamed:@"RC32NLI__LBBJR]ET5I%D{5.jpg"] forState:UIControlStateNormal];
    
    
    _labelZan.frame = CGRectMake(_buttonZan.frame.origin.x + _buttonZan.frame.size.width, _buttonZan.frame.origin.y + 10, 45, 15);
    _labelZan.font = [UIFont systemFontOfSize:12];
    _labelZan.textColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
//    _labelZan.backgroundColor = [UIColor yellowColor];
    
    
    
    
    _buttonLow.frame = CGRectMake(_labelZan.frame.origin.x + _labelZan.frame.size.width + 4, _labelZan.frame.origin.y - 5, 30, 30);
    [_buttonLow setImage:[UIImage imageNamed:@"51FYKZ@K77SS}A0@{G_C]C7.jpg"] forState:UIControlStateNormal];
    
    _labelLow.frame = CGRectMake(_buttonLow.frame.origin.x
                                 + _buttonLow.frame.size.width, _buttonLow.frame.origin.y  + 5, 45, 15);
    _labelLow.font = [UIFont systemFontOfSize:12];
    _labelLow.textColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
//    _labelLow.backgroundColor = [UIColor redColor];
    
    
    
    _buttonHot.frame = CGRectMake(_labelLow.frame.origin.x + _labelLow.frame.size.width + 4, _labelLow.frame.origin.y - 6, 30, 30);
    [_buttonHot setImage:[UIImage imageNamed:@"LK@AF$6X){$X6E2WH3}5MUI.jpg"] forState:UIControlStateNormal];
    
    _labelHot.frame = CGRectMake(_buttonHot.frame.origin.x + _buttonHot.frame.size.width, _buttonHot.frame.origin.y + 6, 45, 15);
    _labelHot.font = [UIFont systemFontOfSize:12];
    _labelHot.textColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1];
    
    

    //    如果评论数组不为空， 则创建评论tableView
    if (self.model.comments.count != 0 ) {
        
        [self createTableViewComment];
        
    }


}


//- (void)drawRect:(CGRect)rect {
//    CGContextRef context = UIGraphicsGetCurrentContext();
//    CGContextSetFillColorWithColor(context, [UIColor clearColor].CGColor);
//    CGContextFillRect(context, rect);
//    //上分割线，
//    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
//    CGContextStrokeRect(context, CGRectMake(5, -1, rect.size.width - 10, 10));
//
//
//    //下分割线
//    CGContextSetStrokeColorWithColor(context, [UIColor blueColor].CGColor);
//    CGContextStrokeRect(context, CGRectMake(5, rect.size.height, rect.size.width - 10, 10));
//}
//


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
